namespace ScrumSprintMonitor.ConfigEditor.Views.SprintConfigurationEditorView
{
	#region #using Directives

	using System;
	using System.Windows;
	using System.Windows.Input;

	using ConfigurationView;

	using Microsoft.Practices.ServiceLocation;

	#endregion

	/// <summary>
	/// Interaction logic for SprintConfigurationEditorWindow.xaml
	/// </summary>
	public partial class SprintConfigurationEditorWindow : Window
	{
		private readonly ISprintConfigurationEditorViewModel model;

		public SprintConfigurationEditorWindow()
		{
			InitializeComponent();
		}

		public SprintConfigurationEditorWindow(ISprintConfigurationEditorViewModel model)
		{
			this.model = model;

			// TODO: Find a better way to reconcile runtime and design time binding
			XamlServiceLocator.Assign<ISprintConfigurationEditorViewModel>(null);
			XamlServiceLocator.Assign(model);

			InitializeComponent();
		}

		public ISprintConfigurationEditorViewModel Model
		{
			get
			{
				if (this.model != null)
				{
					return this.model;
				}

				return ServiceLocator.Current.GetInstance<ISprintConfigurationEditorViewModel>();
			}
		}

		private void Window_Loaded(object sender, RoutedEventArgs e)
		{
			var configurationViewModel = ServiceLocator.Current.GetInstance<IConfigurationViewModel>();
			Model.ChangeServer(configurationViewModel.TfsServerName);
		}

		private void Window_Closed(object sender, EventArgs e)
		{
			if (DialogResult.GetValueOrDefault(false))
			{
				Model.Save();
			}

			XamlServiceLocator.Assign<ISprintConfigurationEditorViewModel>(null);
		}

		private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
		{
			DragMove();
		}
	}
}